package com.yulebaby.callsenter.ui.fragment;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.adapter.CustomerFragmentPagerAdapter;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.SequenceEvent;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.ui.activity.CustomerSearchActivity;
import com.yulebaby.callsenter.ui.base.BaseFragment;
import com.yulebaby.callsenter.utils.DensityUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by zhangbo on 2016/12/11.
 */

public class CustomerFragment extends BaseFragment {

    @BindView(R.id.tab_indicator)
    TabLayout mTabIndicator;
    @BindView(R.id.vp_show)
    ViewPager mVpShow;
    @BindView(R.id.iv_search)
    ImageView mIvSearch;
    @BindView(R.id.ib_sort)
    ImageView mIbSort;
    @BindView(R.id.tb_title)
    Toolbar mTbTitle;

    private Unbinder mUnbinder;
    private PopupWindow popupWindow;

    public static CustomerFragment newInstance() {
        Bundle args = new Bundle();
        CustomerFragment fragment = new CustomerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_return_visit, container, false);
        mUnbinder = ButterKnife.bind(this, view);
        initView(view);
        return view;
    }


    private void initView(View view) {
        mTabIndicator.addTab(mTabIndicator.newTab().setText("所有客户"));
        mTabIndicator.addTab(mTabIndicator.newTab().setText("即将投公海"));
        mTabIndicator.addTab(mTabIndicator.newTab().setText("近期回访"));
    }

    /**
     * 懒加载
     * @param savedInstanceState
     */
    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        mVpShow.setAdapter(new CustomerFragmentPagerAdapter(getChildFragmentManager()));
        mTabIndicator.setupWithViewPager(mVpShow);
        mVpShow.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabIndicator));
        mTabIndicator.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

    /**
     * 初始化排序popupwindow
     */
    private void showSortPopupwindow() {
        View popview = LayoutInflater.from(mActivity).inflate(R.layout.sort_popupwindow, null);
        ImageButton keycustomer_sort = (ImageButton) popview.findViewById(R.id.keycustomer_sort);
        ImageButton time_sort = (ImageButton) popview.findViewById(R.id.time_sort);

        TextView tv_sort = (TextView) popview.findViewById(R.id.tv_sort);
        int tv_sort_size = DensityUtil.px2dip(mActivity, 44);
        tv_sort.setTextSize(tv_sort_size);
        popupWindow = new PopupWindow(popview, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        //设置按钮监听
        keycustomer_sort.setOnClickListener(itemsOnClick);
        time_sort.setOnClickListener(itemsOnClick);

        //设置SelectPicPopupWindow弹出窗体可点击
        popupWindow.setFocusable(true);
        //设置PopupWindow可触摸
        popupWindow.setTouchable(true);
        //设置非PopupWindow区域是否可触摸
        popupWindow.setOutsideTouchable(true);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x60000000);
        //设置PopupWindow弹出窗体的背景
        popupWindow.setBackgroundDrawable(dw);
        popupWindow.showAsDropDown(mTbTitle);//显示在控件下方
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
//                backgroundAlpha(mActivity, 1f);

            }
        });
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            // 这里如果返回true的话，touch事件将被拦截
            // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            public boolean onTouch(View v, MotionEvent event) {

                return false;
            }
        });
    }

    //为弹出窗口内部实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.time_sort:
                    EventBus.getDefault().post(EventFactory.CreateSequenceTypeEvent(SequenceEvent.sequenceByTime));
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                        return;
                    }
                    break;
                case R.id.keycustomer_sort:
                    EventBus.getDefault().post(EventFactory.CreateSequenceTypeEvent(SequenceEvent.sequenceByLevel));
                    if (popupWindow != null && popupWindow.isShowing()) {
                        popupWindow.dismiss();
                        return;
                    }
                    break;
            }
        }
    };

    @OnClick({R.id.iv_search, R.id.ib_sort})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search:
                //跳转到查询的界面
                Intent intent = new Intent(mActivity, CustomerSearchActivity.class);
                intent.putExtra(Constant.CLASS_NAME, true);
                startActivity(intent);
                break;
            case R.id.ib_sort:
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.dismiss();
                    return;
                } else {
                    showSortPopupwindow();
                }
                break;
        }
    }
}
